package com.iss.airsys.controller;

import com.iss.airsys.entity.Guest;
import com.iss.airsys.entity.Room;
import com.iss.airsys.entity.Roomtype;
import com.iss.airsys.model.MyResponseData;
import com.iss.airsys.model.MyResponseData;
import com.iss.airsys.service.GuestService;
import com.iss.airsys.service.RoomfreeService;
import com.iss.airsys.service.RoomtypeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("room/")
public class RoomController {
    private boolean flag=false;


    @Autowired
    RoomfreeService roomfreeService;
    @Autowired
    RoomtypeService roomtypeService;
    @Autowired
    MyResponseData data;
    @Autowired
    GuestService guestService;


    //查询全部未入住房间
    @RequestMapping(value="selectAllForFree",produces = "application/json;charset=utf-8")
    public List<Room> selectAllForFree(@Param("priceFrom")Double priceFrom,@Param("priceTo")Double priceTo){
        List<Room> rooms = roomfreeService.selectAll(priceFrom,priceTo);
        System.out.println(rooms);
        return rooms;
    }

    //查询所有未入住房间号
    @RequestMapping("selectRoomidForfree")
    public MyResponseData selectRoomidForfree(){
        List<String> list= roomfreeService.selectRoomid();
        if (list!=null){
            data.setObject(list);
        }
        return data;
    }

    //查询所有未入住房的id
    @RequestMapping("selectRoomid")
    public MyResponseData selectRoomid(){
        List<String>list = roomfreeService.selectRoomid();
        data.setObject(list);
        return data;
    }
    //通过id查询房间
    @RequestMapping("selectRoomByid")
    public MyResponseData selectRoomByid(String room_id){
        System.out.println("room_no========="+room_id);
        Room room=roomfreeService.selectRoomByid(room_id);
        if (room!=null){
            data.setErrorCode(0);
            data.setObject(room);
        }else{
            data.setErrorCode(-1);
        }
        return data;
    }
    //deleteRoomFree
    @RequestMapping("deleteRoomFree")
    public MyResponseData deleteRoomFree(String []room_ids){
        try{
            if (room_ids[0]!=null){
                flag =roomfreeService.deleteRoom(room_ids);
                if (flag){
                    data.setErrorCode(0);
                    data.setStatus("删除成功!");
                    data.setObject(flag);
                }else{
                    data.setErrorCode(-1);
                    data.setStatus("删除失败!");
                }
            }else{
                data.setErrorCode(-1);
                data.setStatus("请先选中房间!");
            }
        }catch(Exception e){
            System.out.println("我是NullPointerException");
            data.setErrorCode(-1);
            data.setStatus("请先选中房间!");

        }



        return data;
    }
//查询全部的roomtype
    @RequestMapping("selectAllRoomtype")
    public MyResponseData selectAllRoomtype(){
        List<String>list = roomtypeService.selectAll();
        data.setObject(list);
        return data;
    }


//添加房间
    @RequestMapping("insertRoomForfree")
    public MyResponseData insertRoomForfree(Room room){
  //先看主键是否重复
    Room roomid=roomfreeService.selectRoomByid(room.getRoom_id());
    try {
        if (roomid == null) {
            flag = roomfreeService.insertRoom(room);
            if (flag) {
                data.setErrorCode(0);
                data.setStatus("插入成功");
                data.setObject(room);
            } else {
                data.setErrorCode(-1);
                data.setStatus("插入失败");
            }
        } else {
            data.setErrorCode(-2);
            data.setStatus("房间名已存在");
        }
    }catch (Exception e){
        data.setErrorCode(-3);
        data.setStatus("请完善未填写的信息");
    }

     return data;
    }





//更新roomfree
    @RequestMapping("updatetRoomForfree")
    public MyResponseData updatetRoomForfree(Room room){
        flag=roomfreeService.updatetRoom(room);
        if (flag){
            data.setErrorCode(0);
            data.setStatus("更新成功!");
        }else{
            data.setErrorCode(-1);
            data.setStatus("更新失败!");
        }
        return data;
    }

    //退房-把状态改为空闲
    @RequestMapping("checkoutroom")
    public MyResponseData checkoutroom(Integer[] checkout_room){
        System.out.println("Checkout_room======="+checkout_room[0]);
//        try{
            if (checkout_room[0]!=null){
                //修改状态
                int count =roomfreeService.CheckoutRoom(checkout_room);
                //修改退房时间
                Date date = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
                String format = simpleDateFormat.format(date);
                Guest guest = new Guest();
                guest.setGuest_checkouttime(format);
                int count1= guestService.updateCheckouttime( guest.getGuest_checkouttime(),checkout_room[0]);


                if (count>0 && count1>0){
                    data.setErrorCode(0);
                    data.setStatus("退房成功!");
                    data.setObject(flag);
                }else{
                    data.setErrorCode(-1);
                    data.setStatus("退房失败!");
                }
            }else{
                data.setErrorCode(-1);
                data.setStatus("请先选中房间!");
            }
//        }catch(Exception e){
//            System.out.println("我是NullPointerException");
//            data.setErrorCode(-1);
//            data.setStatus("请先选中房间!");
//
//        }
        return data;
    }










}
